<?php

use yii\helpers\Url;
use yii\bootstrap\Alert;
use backend\widgets\GridView;
use yii\widgets\LinkPager;

$this->title = '用户列表';
?>
<style type="text/css">
    th{text-align:center !important;}
</style>
<div class="RightCenter clear">
    <div class="RightTime">
        <div class="Time">
            <h3>用户列表</h3>
        </div>
    </div>
    <div class="Read_date" style="margin-top:20px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ" style="margin-top:12.5px;">
                <div class="Seek">
                    <input type="text" class="form-control AppIN" id="exampleInputAmount" placeholder="搜索昵称" value="<?php echo $select['search']; ?>">
                    <button type="button" class="btn btn-primary SS">搜索</button>
                    <button type="button" class="btn btn-primary CZ">重置</button>
                </div>
            </li>
            <li style="float: right;">
                <div style="margin-right: 30%">
                    共 <?php echo $pagination->totalCount; ?>条数据
                </div>
            </li>
        </ul>
    </div>
    <?php
    if (Yii::$app->getSession()->hasFlash('success')) {
        echo Alert::widget([
            'options' => [
                'class' => 'alert-success', //这里是提示框的class
                'style' => 'width:96%;margin:0 auto;',
            ],
            'body' => Yii::$app->getSession()->getFlash('success'), //消息体
        ]);
    }
    if (Yii::$app->getSession()->hasFlash('error')) {
        echo Alert::widget([
            'options' => [
                'class' => 'alert-error',
                'style' => 'width:96%;margin:0 auto;',
            ],
            'body' => Yii::$app->getSession()->getFlash('error'),
        ]);
    }
    ?>
    <?php
    echo GridView::widget([
        'layout' => "{items}",
        'dataProvider' => $dataProvider,
        'tableOptions' => ['class' => 'Atable'],
        'columns' => [
            [
                'label' => '昵称账号',
                'format'=>'raw',
                'contentOptions' => ['class'=>'TR_Tows'],
                'value' => function($data) 
                {
                    return '<a href="JavaScript:;" class="One">'.$data['user_nickname'].'</a></br><a href="JavaScript:;" class="Tow">'.$data['user_phone'].'</a>';
                }
            ],
            [
                'label' => '已消费/总余额(阅币)',
                'value' => function ($data) 
                {
                    return $data['user_balance_used'] . "/" . $data['user_balance'];
                }
            ],
            [
                'label' => '充值总额(元)',
                'attribute' => 'pay_money',
            ],
            [
                'label' => '充值笔数',
                'attribute' => 'pay_cnt',
            ],
            [
                'label' => '性别/年龄/城市',
                'value' => function ($data) {
                    return ($data['user_sex'] == '1' ? '男' : ($data['user_sex'] == '2' ? '女' : '未知'))
                            . "/" . ($data['user_age'] > 0 ? $data['user_age'] : '未设置')
                            . "/" . ($data['user_province'] != '' ? $data['user_province'] : '未设置');
                }
            ],
            [
                'label' => '注册来源',
                'format' => 'raw',
                'contentOptions' => ['class'=>'TR_Tows'],
                'value' => function ($data) 
                {
                    return '<a href="JavaScript:;" class="One">'.$data['partner'].'</a></br><a href="JavaScript:;" class="Tow">'.$data['channel'].'</a>';
                }
            ],
            [
                'label' => '详细来源',
                'format' => 'raw',
                'contentOptions' => ['class'=>'TR_Tows'],
                'value' => function ($data) 
                {
                    return '<a href="JavaScript:;" class="One">'.$data['novel'].'</a></br>'
                            . '<a href="JavaScript:;" class="Tow">'.($data['user_reg_recommend_sn'] != '' ? $data['user_reg_recommend_sn'] : '无').'</a>';
                }
            ],
            [
                'label' => '账号状态',
                'attribute' => 'user_status',
                'value' => function($data) {
                    return $data['user_status'] == 1 ? '正常' : '封号';
                }
            ],
            [
                'label' => '注册日期',
                'contentOptions' => ['class'=>'TR_Time'],
                'attribute' => 'user_reg_time',
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作', //template
                'template' => "{consume} {recharge} {disabled} {undisabled}",
                'buttons' => [
                    'consume' => function ($url, $data, $key) {
//                        if (Yii::$app->user->can('user.user.consume')) {
                            return "<a href=" . Url::to(['/user/consume', 'uid' => $data['user_id']]) . ">消费记录</a>";
//                        }
                    },
                    'recharge' => function ($url, $data, $key) {
//                        if (Yii::$app->user->can('user.user.recharge')) {
                            return "<a href=" . Url::to(['/user/recharge', 'uid' => $data['user_id']]) . ">充值记录</a>";
//                        }
                    },
                    'disabled' => function ($url, $data, $key) {
//                        if (Yii::$app->user->can('user.user.disabled')) {
                            if($data['user_status'] == 1){
                                return "<a href=" . Url::to(['/user/disabled', 'uid' => $data['user_id']]) . " class=\"LastTd_a\">封号</a>";
                            }
//                        }
                    },
                    'undisabled' => function ($url, $data, $key) {
//                        if (Yii::$app->user->can('user.user.undisabled')) {
                            if($data['user_status'] == 2){
                                return "<a href=" . Url::to(['/user/undisabled', 'uid' => $data['user_id']]) . ">解封</a>";
                            }
//                        }
                    },
                ],
            ],
        ],
    ]);
            ?>
    <div style="float: right;margin-right: 1.7%;">
        <?php echo LinkPager::widget(['pagination' => $pagination]) ?>
    </div>
</div>
<script>
    $(".SS").click(function(){
        var search = $("#exampleInputAmount").val();
        if(search == ''){
            return false;
        }
        var page = 1;
        for(var i=0;i<$(".pagination>li").length;i++){
            if($(".pagination>li").eq(i).hasClass("active")){
                page = i;
            }
        }
        location.href="/user/reader?search="+search+"&page="+page;
    });
    
    $(".CZ").click(function(){
        location.href="/user/reader";
    });

    setTimeout(function () {
        $("#w0").find("button").click();
    }, 3000);

</script>